package com.yaowk.web.mall.clothing.controller;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Member;
import com.yaowk.api.mall.clothing.service.IMemberService;
import com.yaowk.api.mall.clothing.web.MemberModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by yaowk on 17-10-20.
 */
@RestController
@RequestMapping("/mall/clothing/member")
@Api(tags = "会员")
public class MemberController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @ApiOperation("查询/搜索会员列表")
    @GetMapping
    public PageResp<MemberModel> list(String keyword, PageReq pageReq) {
        PageResp<MemberModel> page = memberService.findAllOfWeb(keyword, pageReq);
        return page;
    }

    @ApiOperation("保存会员")
    @PostMapping
    public Member save(Member member) {
        if (!ObjectUtils.isEmpty(member.getPassword())) {
            member.setPassword(passwordEncoder.encode(member.getPassword()));
        }
        member = memberService.save(member);
        return member;
    }

    @ApiOperation("删除会员")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        memberService.deleteById(id);
    }

    @ApiOperation("查看会员详情")
    @GetMapping("/{id}")
    public Member member(@PathVariable Integer id) {
        Member member = memberService.findById(id);
        return member;
    }

}
